﻿/*************************************************************************
 * Copyright 2013, OhZeeCreative, LLC
 * This code is protected by copyright. Unauthorized use is prohibited.
 *************************************************************************/

namespace OhZeeCreative.Windows8.UI.Lynyrd
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Media.Imaging;
    using System.Runtime.InteropServices.WindowsRuntime;

    public class TileWall : BindableBase
    {
        private ImageSource imageSource;
        public ImageSource ImageSource
        {
            get { return this.imageSource; }
            set 
            { 
                if (this.SetProperty<ImageSource>(ref this.imageSource, value))
                {
                    this.ProcessImage();
                } 
            }
        }

        private WriteableBitmap wall;
        private void UpdateWall(WriteableBitmap value)
        {
            this.SetProperty<WriteableBitmap>(ref this.wall, value, "Wall"); 
        }

        public ImageSource Wall
        {
            get { return this.wall; }        
        }

        private double width;
        public double Width
        {
            get { return this.width; }
            set
            {
                if (this.SetProperty<double>(ref this.width, value))
                {
                    this.columns = tileWidth == 0.0 ? 0 : (int) Math.Ceiling(this.width / tileWidth);
                }
            }
        }

        private double height;
        public double Height
        {
            get { return this.height; }
            set
            {
                if (this.SetProperty<double>(ref this.height, value))
                {
                    this.rows = tileHeight == 0.0 ? 0 : (int)Math.Ceiling(this.Height / tileHeight);
                }
            }
        }


        private int tileWidth;
        private int tileHeight;
        private int rows;
        private int columns;


        public TileWall()
        {         
        }


        private void ProcessImage()
        {
            BitmapImage bitmap = this.imageSource as BitmapImage;
            

            if (bitmap == null)
            {
                this.UpdateWall(null);
                return;
            }

            
            bitmap.ImageOpened += bitmap_ImageOpened;

            
                       
            
            //WriteableBitmap writableBitmap = new WriteableBitmap(this.rows * this.tileWidth, this.columns * this.tileHeight);


            //using (Stream pixelStream = writableBitmap.PixelBuffer.AsStream())
            //{
            //    for (int row = 0; row < this.rows; row++)
            //    {
            //        for (int col = 0; col < this.columns; col++)
            //        {
            //            //pixelStream.Write
            //        }
            //    }
            //}

            //// HACK: Not very efficient to redo the writable bitmap everytime.
            //this.UpdateWall(writableBitmap);
        }

        void bitmap_ImageOpened(object sender, RoutedEventArgs e)
        {

            BitmapImage bitmap = (BitmapImage)sender;
            this.tileWidth = bitmap.PixelWidth;
            this.tileHeight = bitmap.PixelHeight;
        }
    }
}
